package com.ofek.eventos;

import java.util.ArrayList;
import java.util.Arrays;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ofek.applicationbase.broadcast.Delegate;
import com.ofek.applicationbase.forms.FieldQConnector;
import com.ofek.applicationbase.forms.FormFragment;
import com.ofek.applicationbase.forms.QuestionViewController;
import com.ofek.applicationbase.forms.SimpleQuestionView;
import com.ofek.applicationbase.forms.question.ConditionalQuestionView;
import com.ofek.applicationbase.forms.question.OptionBoxQuestionView;
import com.ofek.applicationbase.forms.question.TimeQuestionView;
import com.ofek.applicationbase.utils.Log;
import com.ofek.eventos.db.EventosDatabaseService;
import com.ofek.eventos.db.EventosDatabaseService.EventosDatabaseServiceBinder;
import com.ofek.eventos.db.datatypes.Type;
import com.ofek.eventos.db.datatypes.predefined.SearchScheme;

public abstract class SearchDefinerFragment extends FormFragment {
	protected SearchScheme searchData;
	public static final String INTENT_KEY_SEARCH_DEFAULTS="SEARCH_DEFAULTS";
	private ArrayList<Type> types;
	private ServiceConnection service;
	protected EventosDatabaseService eventosService;

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		Log.d("onCreate");
		final Bundle arguments = getArguments();
		if (arguments!=null && arguments.containsKey(INTENT_KEY_SEARCH_DEFAULTS)) 
			searchData = (SearchScheme) arguments.get(INTENT_KEY_SEARCH_DEFAULTS);
		if (searchData==null) searchData = new SearchScheme();
		super.onCreate(savedInstanceState);


		getActivity().bindService(new Intent(getActivity(),EventosDatabaseService.class), service = new ServiceConnection() {



			@Override
			public void onServiceDisconnected(final ComponentName name) {

			}

			@Override
			public void onServiceConnected(final ComponentName name, final IBinder service) {
				final EventosDatabaseService.EventosDatabaseServiceBinder binder = (EventosDatabaseServiceBinder) service;
				eventosService = binder.getEventosService();
				types = eventosService.getTypes();
				initForm(true);
			}
		}, 0);
	}
	@Override
	public void onResume() {
		Log.d("onResume");
		super.onResume();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		getActivity().unbindService(service);
	}
	@Override
	public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
			final Bundle savedInstanceState) {
		Log.d("onCreateView" + super.onCreateView(inflater, container, savedInstanceState));
		return super.onCreateView(inflater, container, savedInstanceState);
	}


	@Override
	protected ArrayList<QuestionViewController> createQuestions() {
		QuestionViewController[] controllers = null;
		final OptionBoxQuestionView optionBoxQuestionView = new OptionBoxQuestionView(R.string.event_type, getActivity(),types.toArray(new Type[types.size()]));
		try {
			ConditionalQuestionView conditional;
			TimeQuestionView fromQuestion;
			TimeQuestionView toQuestion;
			controllers = new QuestionViewController[] {
					new FieldQConnector("owner",searchData,new SimpleQuestionView(R.string.owner,
							InputType.TYPE_CLASS_TEXT, getActivity(),false))
					,new FieldQConnector("startWith", searchData,
							new SimpleQuestionView(R.string.event_title,
									InputType.TYPE_CLASS_TEXT, getActivity(),false))
					,new FieldQConnector("type", searchData, optionBoxQuestionView)
					,new FieldQConnector("careForTime",searchData,
							conditional = new ConditionalQuestionView(R.string.by_time, getActivity()))
					,new FieldQConnector("from", searchData,
							fromQuestion = new TimeQuestionView(R.string.from,getActivity()))
					,new FieldQConnector("to", searchData,
							toQuestion=new TimeQuestionView(R.string.to,getActivity()))

			};
			conditional.addConditionalQuestion(fromQuestion);
			conditional.registerAnswerChangedListener(new Delegate<Object>() {

				@Override
				public void invoke(final Object t) {
					notifyFormHasChanged();
				}
			});
			conditional.addConditionalQuestion(toQuestion);
		} catch (final Exception e) {
			e.printStackTrace();
		}
		return new ArrayList(Arrays.asList(controllers));
	}
}
